/* eslint-disable */
import $ from "jquery";

export  default  {
    /**
     * @Author renminghe
     * @Description // 相应回车键处理input输入
     * @Date 11:29 2018-12-29
     * @Param e Event 键盘事件
     * @Param data Object 目标数据对象
     * @Param parma String 目标数据对象属性
     * @return null
     **/
    changeVal (target, data, paramList) {

        let value = {};

        let i = 0;
        for (i; i < paramList.length - 1; i++) {
            value = value[paramList[i]] || data[paramList[i]] || 0;
        }

        if (target.value) {

            if (target.value.search(/^0\d+\.?\d+$/) > -1 ) {
                target.value  = target.value.substring(1);
            }

            if (target.value.search(/^\.\d{1}$/) > -1) {
                target.value = "0" + target.value + "0";
            }

            else if (target.value.search(/^\.\d{2}$/) > -1) {
                target.value = "0" + target.value;
            }

            else if (target.value.search(/^\d+\.\d{1}$/) > -1) {
                target.value += "0";
            }

            else if (target.value.search(/^\d+\.\d+$/) === -1) {
                target.value += ".00";
            }

            value[paramList[paramList.length - 1]] = parseFloat(parseFloat(target.value).toFixed(2));

            return;
        }

        target.value = value[paramList[paramList.length - 1]].toFixed(2);

    },

    isLCS : (type) => type === "light" || type === "camera" || type === "entry_scene",

    setCookie : (c_name, value, expiredays=7) => {

        const exDate=new Date();

        exDate.setDate(exDate.getDate() + expiredays);

        document.cookie = `${c_name}= ${value};expires=${exDate.toGMTString()}`;
    },

    getCookie : (c_name) => {

        let c_start = document.cookie.indexOf(c_name + "=");

        if (c_start > -1) {
            c_start = c_start + c_name.length + 1;
            let c_end = document.cookie.indexOf(";", c_start);

            if (c_end === -1) {
                c_end = document.cookie.length;
            }

            return document.cookie.substring(c_start, c_end);
        }

        return "";
    },

    paresUrlSearchStr : () => {

        let searchStr = location.search;

        if (!searchStr) return null;

        searchStr = searchStr.substring(1);

        const temArr = searchStr.split("&");
        const resMap = {};

        temArr.forEach(item => {

            let findIndex = item.search(/=/);

            if (findIndex > -1) resMap[item.substring(0, findIndex)] = item.substring(findIndex + 1);
        });

        return resMap;
    },

    getFileType: (fileName) => {

        if (typeof fileName !== 'string') return "";

        const strArr = fileName.match(/.\w*\W*/g);

        return strArr[strArr.length - 1];
    },

    parseFbx: (file, cb) => {

        Editor.readFile(file, false, (data) => {

            const loader = new Halo.FBXLoader();

            const group = loader.parse(data, file.name.substr(0, file.name.length - 4),
                "", group => {
                });

            if(cb) cb(group);
        });
    },

    getUsedModelSet: (type, name, target) => {

        let modelSet = [];

        if (getAttr(type) === null) return modelSet;

        let k = "";
        for (k in target) {
            if (target[k] && target[k].children && target[k].children.length > 0) {

                const childArr = target[k].children;

                childArr.forEach((data, i) => {
                    if (data[getAttr(type)] && data[getAttr(type)].name === name) {
                        modelSet.push({obj: target[k], childIndex: i});
                    }
                });

            }
        }

        return modelSet;

    },

    setUsedModel: (type, obj, target) => {

        if (getAttr(type) === null) return;

        target.forEach(item => {
            item.obj.children[item.childIndex][getAttr(type)] = obj.obj;
        });
    },
}

const getAttr = (key) => {
    const arrtMap = {"geo": "geometry"};
    return arrtMap[key] || null;
};